package com.umfwechat.lightprogram.controller.merchant;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.umfwechat.common.code.RetCode;
import com.umfwechat.common.constant.PropertyConsts;
import com.umfwechat.util.DateTimesUtil;
import com.umfwechat.util.ReqMessageUtil;
import com.umfwechat.util.StringReaderUtils;
import com.umfwechat.util.StringUtil;
import com.umpay.sp.model.common.ReqMessage;
import com.umpay.sp.model.common.ResMessage;
import com.umpay.sp.model.soopay.PayOrder;
import com.umpay.sp.service.soopay.remote.PayOrderServiceRemote;
import com.umpay.sp.xml.XmlData;

@Controller
@RequestMapping("/tradeDetail")
public class TradeDetail {
	private static Logger logger = LoggerFactory.getLogger(TradeDetail.class);
	
	@Resource
	private PayOrderServiceRemote payOrderServiceRemote;
	
	/**
	 * 单笔支付订单详情
	 * @param request
	 * @param resp
	 * @return
	 */
	@RequestMapping(value = "/queryOrderDetail", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject queryYesterdayMerTradeAmountBystores(HttpServletRequest request, HttpServletResponse resp) {
		logger.info("订单详情查询接口 开始");
		String reqJSON=null;
		try {
			reqJSON = (String) request.getAttribute("_requestData");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		JSONObject param=JSONObject.fromObject(reqJSON);
		String tradeNo=(String)param.get("tradeNo");
		JSONObject resultMap=new JSONObject();
		if(StringUtil.isEmpty(tradeNo)){
			logger.info("订单号不能为空！"); 
			resultMap.put("retCode", "0001");
			resultMap.put("retMsg", "U付订单号不能为空！");
			return resultMap;
		}
		try{
			ReqMessage reqMessage = ReqMessageUtil.makeSpReq();
			reqMessage.getReqMap().put(XmlData.TRADENO, tradeNo);
			ResMessage resMessage = payOrderServiceRemote.getPayOrderInfByTradeNo(reqMessage);
			if(resMessage.getRetCode().equals(RetCode.SUCCESS)){
				PayOrder payOrder = (PayOrder)resMessage.getValue(XmlData.PAYORDER);
				if(payOrder != null){
					resultMap.put("tradeDateTime", DateTimesUtil.timestampToStr(payOrder.getInTime(), DateTimesUtil.formatTime15Str));
					resultMap.put("orderAmount", payOrder.getAmount());
					
					String productId = payOrder.getProductId();
					if(PropertyConsts.PRODUCTID_WECHAT.indexOf(productId) >= 0){//立马付-微信
						resultMap.put("payer", "微信用户");
						resultMap.put("payType", "1");
				    }else if(PropertyConsts.PRODUCTID_ALIPAY.indexOf(productId) >= 0){//立马付-支付宝
						resultMap.put("payer", "支付宝用户");
						resultMap.put("payType", "2");
				    }else if(PropertyConsts.PRODUCTID_WECHATPAY.indexOf(productId) >= 0){//微信公众号支付
						resultMap.put("payer", "微信用户");
						resultMap.put("payType", "3");
					}
					else{
						resultMap.put("payer", "");
						resultMap.put("payType", "");
					}
					resultMap.put("tradeNo", payOrder.getTradeNo());
					
					logger.info("订单详细信息查询成功！");
		        	resultMap.put("retCode", RetCode.SUCCESS);
		    		resultMap.put("retMsg", "订单详细信息查询成功！");
		    		
		    		return resultMap;
				}
			}
			logger.info("订单详情查询失败！");
        	resultMap.put("retCode", "0001");
    		resultMap.put("retMsg", "订单详情查询失败！");
    		return resultMap;
		} catch (Exception e) {
			logger.info("订单详情查询失败！");
        	resultMap.put("retCode", "0001");
    		resultMap.put("retMsg", "订单详情查询失败！");
    		return resultMap;
		}
	}
}